class Node {
    constructor(element) {
        this.element = element;
        this.left = null;
        this.right = null;
    }
}
class Tree {
    constructor() {
        this.root = null;
    }
    //插入
    insert(element) {
        let node = new Node(element)
        //树为空
        if (this.root === null) {
            this.root = node
        }
        else {
            //树不为空
            this.insertNode(node, this.root)
        }
    }
    insertNode(node, rootNode) {
        //判断插入值和父级节点的大小
        if (node.element < rootNode.element) {
            if (rootNode.left === null) {
                rootNode.left = node
            } else {
                this.insertNode(node, rootNode.left)
            }
        } else {
            if (rootNode.right === null) {
                rootNode.right = node
            } else {
                this.insertNode(node, rootNode.right)
            }
        }
    }
    //最小值
    min() {
        return this.minNode(this.root)
    }
    minNode(rootNode) {
        while (rootNode.left != null) {
            rootNode = rootNode.left
        }
        return rootNode
    }
    //最大值
    max() {
        return this.maxNode(this.root)
    }
    maxNode(rootNode) {
        while (rootNode.right != null) {
            rootNode = rootNode.right
        }
        return rootNode
    }
    //找到指定值
    search(element) {
        return this.searchNode(element, this.root)
    }
    searchNode(element, rootNode) {
        //树为空
        if (rootNode === null) {
            return false
        }

        if (element < rootNode.element) {
            return this.searchNode(element, rooNode.left)
        } else if (element > rootNode.element) {
            return this.searchNode(element, rooNode.right)
        } else {
            return true
        }
    }
    //中序遍历
    inOrderTraverse(callback) {
        this.inOrderTraverseNode(this.root, callback)
    }
    inOrderTraverseNode(node,callback) {
        if (node != null) {
            this.inOrderTraverseNode(node.left,callback)
            callback(node.element)
            this.inOrderTraverseNode(node.right,callback)
        }
    }
}
let tree = new Tree();
tree.insert(11)
tree.insert(7)
tree.insert(5)
tree.insert(9)
tree.insert(3)
tree.insert(6)
tree.insert(8)
tree.insert(10)
tree.insert(15)
tree.insert(13)
tree.insert(12)
tree.insert(14)
tree.insert(20)
tree.insert(18)
tree.insert(25)
let callback = (element) => {
    console.log(element)
}
console.log(tree)
tree.inOrderTraverse(callback)